import type {ColumnType} from "ant-design-vue/lib/table/interface";
import type {CatColumn} from "./column/CatColumn";
import type {ActionColumn} from "./column/ActionColumn";
import type {DictColumn} from "@/components/CatTable/column/DictColumn";
import type {NumberColumn} from "@/components/CatTable/column/NumberColumn";
import NumberUtils from "@/utils/NumberUtils";

export const ColumnTypes = {
    get text(): ColumnType {
        return {
            minWidth: 200,
            maxWidth: 400,
            ellipsis: true,
            sorter: true,
        }
    },
    get dict(): DictColumn {
        return {
            width: 120,
            minWidth: 80,
            align: 'left',
            sorter: true
        }
    },
    get number(): NumberColumn {
        return {
            width: 100,
            minWidth: 80,
            align: 'right',
            resizable: true,
            sorter: true,
            useGrouping: false,
            minDigits: 0,
            maxDigits: 1,
            customRender: ({value, column}) => {
                if (NumberUtils.isNumber(value)) {
                    const numberColumn = <NumberColumn>column;
                    return Intl.NumberFormat('zh-Hans-CN', {
                        useGrouping: false,
                        minimumFractionDigits: numberColumn.minDigits,
                        maximumFractionDigits: numberColumn.maxDigits
                    }).format(value);
                }
                return '';
            }
        }
    },
    get datetime(): ColumnType {
        return {
            width: 180,
            align: 'left',
            resizable: false,
            sorter: true
        }
    },
    get date(): ColumnType {
        return {
            width: 120,
            align: 'left',
            resizable: false,
            sorter: true
        }
    },
    get time(): ColumnType {
        return {
            width: 120,
            align: 'left',
            resizable: false,
            sorter: true
        }
    },
    get switch(): ColumnType {
        return {
            width: 80,
            align: 'left',
            resizable: false,
            sorter: true
        }
    },
    get action(): ActionColumn {
        return {
            width: 140,
            align: 'left',
            resizable: false,
            fixed: 'right',
            items: []
        }
    }
}

export type ColumnTypeName = keyof typeof ColumnTypes;

export function defaultColumnProperties(columns: CatColumn[]) {
    for (let i = 0; i < columns.length; i++) {
        const column = columns[i]
        column.type ??= 'text';
        columns[i] = {...ColumnTypes[column.type], ...column}
        // action，特殊处理
        if (column.type === "action") {
            const action = <ActionColumn>column;
            if (action.items.length >= 3 || action.more) {
                columns[i].width = 180;
            }
        }
    }
}
